<?php


namespace app\admin\controller\School;


use app\admin\controller\BaseController;
use app\admin\model\School\SchoolModel;
use app\Utils\ImageUtil;
use app\Utils\Util;
use think\facade\Db;
use think\Request;

class School extends BaseController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getSchoolList(Request $request)
    {
        $where = [];
        $param = $request->param();
        $page = $param['page'];
        $limit = $param['limit'];
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if(empty($page)){
            $page = 1;
        }
        if(empty($limit)){
            $limit = 10;
        }
        if(!empty($is_show)){
            if($is_show == 1){
                array_push($where,['s.is_show','=',1]);
            }
            if($is_show == 2){
                array_push($where,['s.is_show','=',0]);
            }
        }
        $list = SchoolModel::getSchoolListWhere($page,$limit,$where,'s.*,sa.area_name');
        $count = SchoolModel::getSchoolCountWhere($where);
        foreach ($list as &$item){
            $item['img'] = ImageUtil::i()->replaceUrl($item['img']);
            $images = !empty($item['slider_img']) ? json_decode($item['slider_img'],true) : [];
            foreach ($images as &$image){
                $image = ImageUtil::i()->replaceUrl($image);
            }
            $item['slider_img'] = $images;
            $item['details'] = !empty($item['details']) ? htmlspecialchars_decode($item['details']) : "";
        }
        unset($item);

        return Util::returnPageJson(self::CODE_DATA,$count,$list,"");

    }

    /**
     * 添加
     * @param Request $request
     * @return array
     */
    public function addSchool(Request $request)
    {
        $param = $request->param();
        $account = !empty($param['account']) ? $param['account'] : "";
        $password = !empty($param['password']) ? $param['password'] : "";
        $school_name = !empty($param['school_name']) ? $param['school_name'] : "";
        $brief = !empty($param['brief']) ? $param['brief'] : "";
        $details = !empty($param['details']) ? htmlspecialchars($param['details']) : "";
        $address_name = !empty($param['address_name']) ? $param['address_name'] : "";
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";

        $list = SchoolModel::getSchoolWhereMore([['account','=',$param['account']]]);
        if(!empty($list)){
            return Util::returnJson(self::CODE_ERROR, null, "学校已存在");
        }

        //图片上传
        if (!empty($request->file('file'))) {
            $file = $request->file('file');
            $path = "/school/img/";
            $img = ImageUtil::i()->upload($file, $path);
        }

        //多图
        if (!empty($request->file('files'))) {
            $files = $request->file('files');
            $path = "/school/slider_img/";
            $urls = ImageUtil::i()->upload($files, $path);
        }

        if (!empty($urls)) {
            $data['slider_img'] = json_encode($urls);
        }

        $data = [
            'account'=>$account,
            'password'=>$password,
            'school_name'=>$school_name,
            'img'=>!empty($img) ? $img : "",
            'brief'=>$brief,
            'details'=>$details,
            'address_name'=>$address_name,
            'area_id'=>!empty($param['area_id']) ? $param['area_id'] : 0,
            'up_time'=>date("Y-m-d H:i:s",time()),
            'add_time'=>date("Y-m-d H:i:s",time())
        ];

        if(!empty($is_show) && $is_show == "on"){
            $data['is_show'] = 1;
        }else{
            $data['is_show'] = 0;
        }
        $id = SchoolModel::addSchool($data);
        if($id){
            return Util::returnJson(self::CODE_SUCCESS,$id,"添加成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"添加失败");
        }
    }

    /**
     * 更新
     * @param Request $request
     * @return array
     */
    public function editSchool(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        $password = !empty($param['password']) ? $param['password'] : "";
        $school_name = !empty($param['school_name']) ? $param['school_name'] : "";
        $brief = !empty($param['brief']) ? $param['brief'] : "";
        $details = !empty($param['details']) ? htmlspecialchars($param['details']) : "";
        $address_name = !empty($param['address_name']) ? $param['address_name'] : "";
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        if(empty($id)){
            return Util::returnJson(self::CODE_ERROR,null,"参数错误");
        }

        $save = [
            'school_name'=>$school_name,
            'brief'=>$brief,
            'details'=>$details,
            'address_name'=>$address_name,
            'area_id'=>!empty($param['area_id']) ? $param['area_id'] : 0,
            'up_time'=>date("Y-m-d H:i:s",time())
        ];

        if(!empty($password)){
            $save['password'] = md5($password);
        }

        //图片上传
        if (!empty($request->file('file'))) {
            $file = $request->file('file');
            $path = "/school/img/";
            $img = ImageUtil::i()->upload($file, $path);
            $save['img'] = $img;
        }

        //多图
        if (!empty($request->file('files'))) {
            $files = $request->file('files');
            $path = "/school/img/";
            $urls = ImageUtil::i()->upload($files, $path);
        }

        if (!empty($urls)) {
            $save['slider_img'] = json_encode($urls);
        }

        if(!empty($is_show)){
            if($is_show == "on"){
                $save['is_show'] = 1;
            }else{
                $save['is_show'] = 0;
            }
        }else{
            $save['is_show'] = 0;
        }
        $flag = SchoolModel::updateSchoolWhere([['id','=',$id]],$save);
        if($flag){
            return Util::returnJson(self::CODE_SUCCESS,null,"修改成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"修改失败");
        }
    }

    /**
     * 删除
     * @param Request $request
     * @return array
     */
    public function delSchool(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        //开启事务
        Db::startTrans();
        try{
            //中间逻辑代码
            $flag = SchoolModel::delSchoolWhere([['id','=',$id]]);
            if($flag){
                //提交事务
                Db::commit();
                return Util::returnJson(self::CODE_SUCCESS,null,"删除成功");
            }else{
                Db::rollBack();
                return Util::returnJson(self::CODE_ERROR,null,"删除失败4");
            }
        }catch (\Exception $e) {
            //接收异常处理并回滚
            //回滚事务
            Db::rollBack();
            return Util::returnJson(self::CODE_ERROR,$e,"删除失败1");
        }
    }

    /**
     * 获取多条数据
     * @param Request $request
     * @return array
     */
    public function getSchoolMore(Request $request)
    {
        $list = SchoolModel::getSchoolWhereMore();
        return Util::returnJson(self::CODE_DATA,$list);
    }

}